<template>
  <div class="login_container">
    <!-- 登陆盒子 -->
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatat_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登陆表单 -->
      <a-form-model
        ref="formLoginRef"
        :rules="formLoginRules"
        class="login_form"
        layout="vertical"
        :model="formLogin"
        @submit="handleSubmit('formLoginRef')"
        @submit.native.prevent
      >
        <a-form-model-item prop="username">
          <a-input v-model="formLogin.username" placeholder="Username">
            <a-icon slot="prefix" type="user" style="color:rgba(0,0,0,.25)" />
          </a-input>
        </a-form-model-item>
        <a-form-model-item prop="password">
          <a-input
            v-model="formLogin.password"
            type="password"
            placeholder="Password"
            autocomplete="off"
          >
            <a-icon slot="prefix" type="lock" style="color:rgba(0,0,0,.25)" />
          </a-input>
        </a-form-model-item>
        <a-form-model-item class="btns">
          <a-button
            type="primary"
            html-type="submit"
            :disabled="formLogin.username === '' || formLogin.password === ''"
          >
            登录
          </a-button>
          <a-button
            style="margin-left: 10px;"
            @click="resetForm('formLoginRef')"
          >
            重置
          </a-button>
        </a-form-model-item>
      </a-form-model>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      formLogin: {
        username: 'admin',
        password: '123456'
      },
      formLoginRules: {
        user: [
          { required: true, message: '请输入登录名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            whitespace: true,
            message: '登录名长度在3到10个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '密码长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    handleSubmit (formNameRef) {
      this.$refs[formNameRef].validate(async valid => {
        if (!valid) {
          return
        }
        const { data: res } = await this.$http.post('login', this.formLogin)
        if (res.meta.status !== 200) {
          return this.$message.error('登陆失败,错误:' + res.meta.status)
        }
        this.$message.success('登陆成功')
        console.log(res)
        window.sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    },
    resetForm (formNameRef) {
      this.$refs[formNameRef].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
body{
  background-color:cadetblue;
}
.login_container {
  background-color: #2b5b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  box-shadow: 0 0 10px #ddd;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatat_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
